if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})

function ready() {
    var user = null, 
        thisPage = null,
        requestData = {
          index:1,
          size:10,
        },
        html = [];

    var couponPage = function () {
      this.init();
    };
    couponPage.prototype = {
        init: function () {
          this.initData();
          this.addScroll();
          this.bindEvent();
        },
        initData: function () {
          user = getLocalStorage(STORAGE_MANAGER.user);
          if( !user ){
            redirect("../account/login.html");
            return;
          }
          this.initCoupon("renovate");
        },
        addScroll:function(){
            _pullUp = $("#pullUp");
            _pullDown = $("#pullDown");
            _myscroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            _myscroll.on("scrollEnd", function () {
              console.log("this.y=" + this.y);
              console.log("this.maxScrollY=" + this.maxScrollY);
              if ((this.y < 0) && (this.y == this.maxScrollY)) {
                  console.log("滚动到底部");
                  _pullUp.show();
                  setTimeout(function () {
                      requestData.index++;
                      thisPage.initCoupon("add");
                  }, 500);
              } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                  console.log("滚动到顶部");
                  _pullDown.show();
                  setTimeout(function () {
                      requestData.index = 0;
                      thisPage.initCoupon("renovate");
                  }, 500);
              }
          });
        },
        initCoupon:function(action){
          $.ajax({
            type:"POST",
            url: CONFIG_MANAGER.RequstUrl + "v1/member/asset/coupon/" + user.id,
            async:true,
            data:requestData,
            headers:{ token:user.token  }
          }).done(function(data){
            thisPage.loadCoupon( JSON.parse( data ),action );
          }).fail(function(data){
            var infor = JSON.parse(data);
            $.toast(infor.message);
          });          
        },
        loadCoupon:function(data, action){
          var coupon = data.data;
          if( "renovate" == action && 0 == coupon.length ){
            $("#main").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
          }else{
            html = [];
            $.each(coupon, function(i, item) {
              var beginTime = toDateString( item.begin_date, 'yyyy-MM-dd');
              var endTime = toDateString( item.end_date, 'yyyy-MM-dd');
              var is_Overdate = ([1,2,3].indexOf(item.status) > -1) ? true:false;
              var status_text = "";
              switch (item.status){
                case 0:
                  status_text = '未使用';
                  break;
                case 1:
                  status_text = '已使用';
                  break;
                case 2:
                  status_text = '已过期';
                  break;
                case 3:
                  status_text = '已取消';
                  break;
                default:
                  break;
              }
              if(is_Overdate){
                html.push('<div class="yhuiqbg yhuiqbg1 ddnybbt">');
                html.push('<p class="dfktbdw" style="background:#c6c6c6">');
                html.push(status_text)
                html.push('</p>');
                html.push('<p class="yhqjine yhqjine1">&yen;'+ item.face_value +'</p>');
                html.push('<div class="yhqgz">');
                html.push('<p class="yhqje yhqje1">'+ item.face_value +'元优惠券</p>');
                html.push('<p class="yhqtj yhqtj1"><b></b>'+ item.title +'</p>');
                html.push('<p class="yhqtj yhqtj1"><b></b>有限期限：'+ beginTime +'至'+ endTime +'</p>');
                html.push('</div>');
                html.push('</div>');
              }else{
                html.push('<div class="yhuiqbg ddnybbt">');
                html.push('<p class="dfktbdw">');
                html.push(status_text);
                html.push('</p>');
                html.push('<p class="yhqjine">&yen'+ item.face_value +'</p>');
                html.push('<div class="yhqgz">');
                html.push('<p class="yhqje">'+ item.face_value +'元优惠券</p>');
                html.push('<p class="yhqtj"><b></b>'+ item.title +'</p>');
                html.push('<p class="yhqtj"><b></b>有限期限：'+ beginTime +'至'+ endTime +'</p>');
                html.push('</div>');
                html.push('</div>');              
              }  
            });            
          }
          if(html.length>0){
	        if("add" == action){
	        	$(html.join('')).appendTo($("#couponList"));
	        }else{
	            $("#couponList").html(html.join(''));
	        }
          }
          setTimeout(function () {
              _myscroll.refresh();
              _pullUp.hide();
              _pullDown.hide();
          }, 100);
        },
        bindEvent: function () {

            //回退
            $("#back").on("click", function () {
            	if(AGENT_CLIENT.APP){
					plus.webview.getWebviewById("../mine/mylife.html").evalJS("reloadPage()");
					plus.webview.getWebviewById("../home/main.html").evalJS("redirectTab(4)");
            	}else{
                	redirect("mylife.html");
               	}
            });
            
            $("#active").on("click", function(){
              redirect("../finance/activateCoupon.html");
            });
        }
    };
    
    thisPage = new couponPage();
}
